package com.sky.task;

import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.annotation.Schedules;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Component

public class OrderTask {
@Autowired
    OrderMapper orderMapper;
//每分钟执行一次
//@Scheduled(cron = "0 * * * * ?")
//@Scheduled(cron = "0/5 * * * * ?")
    public void noPay(){
//        log.info("如果订单支付超时则取消订单并把状态修改为已取消",LocalDateTime.now());
        //设置支付时间不能超过15分钟超过则改变状态
        LocalDateTime localDateTime = LocalDateTime.now().plusMinutes(-15);

        List<Orders> ordersList=orderMapper.getByStatusAndCreatTimeLT(Orders.PENDING_PAYMENT,localDateTime);


        if (ordersList!=null&&ordersList.size()>0){
            for (Orders orders : ordersList) {
                orders.setStatus(Orders.COMPLETED);
                orders.setCancelReason("订单支付超时");
                orders.setCancelTime(LocalDateTime.now());

                orderMapper.update(orders);
            }
        }
    }
    //每天凌晨一点
   // @Scheduled(cron = "0 0 1 * * ?")
//    @Scheduled(cron = "5/10 * * * * ?")
    public void noCancel(){
        log.info("如果订单过了打样后一小时还处于配送中，则改变状态为已完成",LocalDateTime.now());
        //设置不能超过一定时间
        LocalDateTime localDateTime = LocalDateTime.now().plusMinutes(-60);

        List<Orders> ordersList=orderMapper.getByStatusAndCreatTimeLT(Orders.DELIVERY_IN_PROGRESS,localDateTime);


        if (ordersList!=null&&ordersList.size()>0){
            for (Orders orders : ordersList) {
                orders.setStatus(Orders.CANCELLED);


                orderMapper.update(orders);
            }
        }
    }
}
